<?php
/*
Plugin Name: Bitty Browser
Plugin URI: http://www.bitty.com
Description: Bitty is the little browser that goes on any Web page.
Author: Turnstyle
Version: 1.0
Author URI: http://www.bitty.com
*/

/*
Bitty Browser (http://www.bitty.com)
Copyright (c)2005-2006, Turnstyle.
All Rights Reserved.
Patent Pending.
*/


function widget_sbitty_init() {

	if (!function_exists('register_sidebar_widget')) {return;}

	function widget_sbitty($args) {
		
		extract($args);

		$options = get_bitty_settings();

		$title = $options['title'];

		echo $before_widget . $before_title . $title . $after_title;

		$bbqs = "";
		$bbqs = buildqs($bbqs,"contenttype",$options['contenttype']);
		$bbqs = buildqs($bbqs,"contentvalue",$options['contentvalue']);
		$bbqs = buildqs($bbqs,"title",$options['title']);
		$bbqs = buildqs($bbqs,"height",$options['height']);
		$bbqs = buildqs($bbqs,"titlebar",$options['titlebar']);
		$bbqs = buildqs($bbqs,"textlabels",$options['textlabels']);
		$bbqs = buildqs($bbqs,"searchbar",$options['searchbar']);
		$bbqs = buildqs($bbqs,"plugin","wordpress");

		$bbhi = $options['height'];
		if ($bbhi == "") {$bbhi = 400;}
		if (is_numeric($bbhi)) {$bbhi .= "px";}

?>
<!-- BITTY BROWSER : WWW.BITTY.COM : {BEGIN} -->
<table cellspacing="0" style="width:100% !important;background:#999 !important;padding:0px !important;margin:0px !important;border:0px !important;border-collapse:collapse !important"><tr><td style="background:#999 !important;padding:1px !important;margin:0px !important;border:0px !important;"><iframe src="http://b1.bitty.com/b2browser/<?php echo $bbqs; ?>" scrolling="no" frameborder="0" style="display:block !important;width:100% !important;height:<?php echo $bbhi; ?> !important;background:#D4D0C8 !important;padding:0px !important;margin:0px !important;border:0px !important;"><a href="http://www.bitty.com/">Bitty Browser</a> (iframes required)</iframe></td></tr></table>
<!-- BITTY BROWSER : WWW.BITTY.COM : {END} -->
<?php

		echo $after_widget;
	}



	function widget_sbitty_control() {

		$options = get_bitty_settings();

		if ( $_POST['sbitty-submit'] ) {
			// Remember to sanitize and format use input appropriately.
			$options['title'] = strip_tags(stripslashes($_POST['sbitty-title']));
			//$options['width'] = strip_tags(stripslashes($_POST['sbitty-width']));
			$options['height'] = strip_tags(stripslashes($_POST['sbitty-height']));
			$options['titlebar'] = strip_tags(stripslashes($_POST['sbitty-titlebar']));
			$options['textlabels'] = strip_tags(stripslashes($_POST['sbitty-textlabels']));
			$options['searchbar'] = strip_tags(stripslashes($_POST['sbitty-searchbar']));
			$options['contenttype'] = strip_tags(stripslashes($_POST['sbitty-contenttype']));
			$options['contentvalue'] = strip_tags(stripslashes($_POST['sbitty-contentvalue']));

			$options['buildID'] = "47ED87FD641E47DEAFB5CF995C6761E9";

			update_option('widget_sbitty', $options);

			$options = get_bitty_settings();
		}


?><table align="center">

	<tr><td align="right">Title:</td><td><input id="sbitty-title" name="sbitty-title" type="text" value="<?php echo htmlspecialchars($options['title']); ?>" style="width:280px;" /></td></tr>

	<tr><td align="right" style="white-space:nowrap;">Title Bar:</td><td><select id="sbitty-titlebar" name="sbitty-titlebar"><option value="on">on</option><option value="off"<?php if ($options['titlebar'] == "off") {echo " selected";} ?>>off</option></select></td></tr>
		
	<tr><td align="right" style="white-space:nowrap;">Button Bar:</td><td><select id="sbitty-textlabels" name="sbitty-textlabels"><option value="on">icon + text</option><option value="off"<?php if ($options['textlabels'] == "off") {echo " selected";} ?>>icon only</option></select></td></tr>

	<tr><td align="right" style="white-space:nowrap;">Search Bar:</td><td><select id="sbitty-searchbar" name="sbitty-searchbar"><option value="on">on</option><option value="off"<?php if ($options['searchbar'] == "off") {echo " selected";} ?>>off</option></select></td></tr>

	<tr><td align="right">Height:</td><td><input id="sbitty-height" name="sbitty-height" type="text" value="<?php echo htmlspecialchars($options['height']) ?>" style="width:70px;" /></td></tr>

	<tr><td align="right" style="white-space:nowrap;">Home Page:</td><td><select id="sbitty-contenttype" name="sbitty-contenttype"><?php

		$bborm = $options['contenttype'];
		bboptrow ("", "{Bitty Default}", $bborm);
		bboptrow ("delicious;tag", "del.icio.us : tag", $bborm);
		bboptrow ("delicious;username", "del.icio.us : username", $bborm);
		bboptrow ("digg;search", "digg : search", $bborm);
		bboptrow ("digg;username", "digg : username", $bborm);
		bboptrow ("technorati;tag", "technorati : tag", $bborm);
		bboptrow ("flickr;tag", "flickr : tag", $bborm);
		bboptrow ("flickr;pool", "flickr : pool", $bborm);
		bboptrow ("bloglines;username", "bloglines : username", $bborm);
		bboptrow ("google:xhtml:web;search", "Google : squeezed", $bborm);
		bboptrow ("google;search", "Google : full size", $bborm);
		bboptrow ("google:news;search", "Google : news", $bborm);
		bboptrow ("google:blogs;search", "Google : blogs", $bborm);
		bboptrow ("website", "Web site URL", $bborm);
		bboptrow ("rssfeed", "RSS feed URL", $bborm);
		bboptrow ("podcast", "Podcast URL", $bborm);
		bboptrow ("opml", "OPML file URL", $bborm);

	?></select></td></tr>

	<tr><td align="right">&gt;&gt;&gt;</td><td><input id="sbitty-contentvalue" name="sbitty-contentvalue" type="text" value="<?php echo htmlspecialchars($options['contentvalue']); ?>" style="width:280px;" /></td></tr>

	<tr><td></td><td><input type="hidden" id="sbitty-submit" name="sbitty-submit" value="1" /><input type="submit" value="Save" /></td></tr>

</table><?php


	}



	function get_bitty_settings () {

		$gbsopt = get_option('widget_sbitty');

		if ($gbsopt['buildID'] != "47ED87FD641E47DEAFB5CF995C6761E9") {

			$gbsopt["title"] = "";
			$gbsopt["titlebar"] = "";
			$gbsopt["textlabels"] = "";
			$gbsopt["searchbar"] = "";
			$gbsopt["height"] = "";
			$gbsopt["contenttype"] = "";
			$gbsopt["contentvalue"] = "";
		}

		if ($gbsopt["title"] == "") {$gbsopt["title"] = "Bitty Browser";}
		if ($gbsopt["height"] == "") {$gbsopt["height"] = "400";}
		if ($gbsopt["contenttype"] == "") {$gbsopt["contentvalue"] = "";}

		return($gbsopt);
	}

	function buildqs ($BQS_qs,$BQS_var,$BQS_val) {
		if (($BQS_var == "") || ($BQS_val == ""))
			return($BQS_qs);
		else if ($BQS_qs == "")
			return("?" . $BQS_var . "=" . urlencode($BQS_val));
		else
			return($BQS_qs . "&" . $BQS_var . "=" . urlencode($BQS_val));
	}

	function bboptrow ($bborv, $bborl, $bborm) {
		$tmp = '<option value="' . $bborv . '"';
		if ($bborv == $bborm) {$tmp .= " selected";}
		$tmp .= '>' . $bborl . '</option>';
		echo $tmp;
	}



	register_sidebar_widget('Bitty Browser', 'widget_sbitty');

	register_widget_control('Bitty Browser', 'widget_sbitty_control', 400, 300);
}

add_action('plugins_loaded', 'widget_sbitty_init');

?>